package com.zxl.live.screen.call.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxl.live.screen.R;
import com.zxl.live.screen.call.helper.ContactHelper;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.tools.common.ApplicationHelper;

import java.io.InputStream;

/**
 * Created by Administrator on 2017/12/10.
 */

public class CallUseInfoWidget extends LinearLayout {

    private ImageView mIconView;

    private TextView mNameView;

    private TextView mNumberView;

    public CallUseInfoWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        inflate(getContext(), R.layout.widget_call_use_info, this);
        mIconView = (ImageView) findViewById(R.id.caller_avatar);
        mNameView = (TextView) findViewById(R.id.caller_name);
        mNumberView = (TextView) findViewById(R.id.caller_number);
    }

    public void refreshTheme(ThemeBean themeBean) {
        mIconView.setImageResource(getResources().getIdentifier(themeBean.defaultIcon, "drawable", getContext().getPackageName()));
        mNameView.setText(themeBean.defaultName);
        mNumberView.setText(themeBean.defaultNumber);
    }

    public TextView getmNumberView() {
        return mNumberView;
    }

    public ImageView getmIconView() {
        return mIconView;
    }

    public TextView getmNameView() {
        return mNameView;
    }

    public void setNumber(final String number) {
        mNameView.setText("");
        mNumberView.setText(number);
        mIconView.setImageResource(getContext().getResources().getIdentifier("face_" + getNumberFaceIcon(number), "drawable", getContext().getPackageName()));
        ApplicationHelper.postThread(new Runnable() {
            @Override
            public void run() {
                final ContactHelper.ContactUser contactUser = ContactHelper.fetchContactIdFromPhoneNumber(getContext(), number);
                if (null != contactUser) {
                    ApplicationHelper.postRunInUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mNameView.setText(contactUser.getUserName());
                        }
                    });

                    if (!TextUtils.isEmpty(contactUser.getPhotoUri())) {
                        InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(ApplicationHelper.getContext().getContentResolver(), Uri.parse(contactUser.getPhotoUri()));
                        final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        if (null != bitmap) {
                            ApplicationHelper.postRunInUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mIconView.setImageBitmap(bitmap);
                                }
                            });
                        }
                    }
                }
            }
        });
    }

    public static int getNumberFaceIcon(String number) {
        try {
            for (int i = number.length() - 1; i >=0 ; i--) {
                int value = Character.getNumericValue(number.charAt(i));
                if (value >= 1 && value <= 8) {
                    return value;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    public void setDefaultNumber() {
        mNameView.setText(R.string.acb_phone_caller_default_name);
        mNumberView.setText(R.string.acb_phone_caller_default_number);
        mIconView.setImageResource(R.drawable.acb_phone_default_caller_avatar);
    }
}
